package com.pmcc.yn.service;

import com.pmcc.utils.AntdPage;
import com.pmcc.utils.BeanUtils;
import com.pmcc.utils.ResultBean;
import com.pmcc.yn.entity.*;
import com.pmcc.yn.repository.AttentionInfoRepository;
import com.pmcc.yn.repository.EntryInfoRepository;
import com.pmcc.yn.repository.OrderListRepository;
import com.pmcc.yn.repository.TechnicianInfoRepository;
import com.pmcc.yn.vo.EntryAndAttentionNumVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;

/**
 * Created by liliguo on 2019/1/14.
 */
@Service
@Transactional
public class EntryInfoService {

    @Autowired
    EntryInfoRepository repository;
    @Autowired
    TechnicianInfoRepository technicianInfoRepository;
    @Autowired
    OrderListRepository orderListRepository;
    @Autowired
    AttentionInfoRepository attentionInfoRepository;

    public ResultBean showList(AntdPage antdPage , EntryInfo model) throws Exception{
        ResultBean resultBean=new ResultBean();
        Page page=repository.findAll(this.getSpecification(model),antdPage.getPageRequest());

        resultBean.setPage(antdPage.getDefaultCurrent());
        resultBean.setTotal(page.getTotalElements());
        resultBean.setResultCode(ResultBean.SUCCESS);
        resultBean.setPageSize(antdPage.getPageSize());
        resultBean.setRows(page.getContent());
        return resultBean;
    }

    public Specification<EntryInfo> getSpecification(EntryInfo model) {
        return new Specification<EntryInfo>() {
            @Override
            public Predicate toPredicate(Root<EntryInfo> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> list = new ArrayList<Predicate>();
                Predicate[] p = new Predicate[list.size()];
                return builder.and(list.toArray(p));
            }
        };
    }

    /**
     * 新增
     * @return
     */
    public ResultBean add(EntryInfo model){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try{
            model.setRegistrationTime(new Date());
            repository.saveAndFlush(model);
            resultBean.setResultCode(ResultBean.SUCCESS);
            resultBean.setResultDesc("新增成功");
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }


    /**
     * 修改
     * @param map
     * @return
     */
    public ResultBean update(Map map) {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            EntryInfo c = repository.findById(map.get("id").toString());
            BeanUtils.setBeanData(c,map);
            repository.saveAndFlush(c);
            resultBean.setResultCode(ResultBean.SUCCESS);
            resultBean.setObject(c);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public ResultBean deleteById(String id) {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            repository.delete(id);
            resultBean.setResultCode(ResultBean.SUCCESS);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    /**
     * 传入订单id，查询报名的技师列表，技师按照评分进行排序
     *  根据报名表 根据订单id查询出技师详情 根据评分排序
     *
     */
    public ResultBean personList(String orderid) {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        TechnicianInfo technicianInfo = new TechnicianInfo();
        List<TechnicianInfo> technicianInfoList = new ArrayList<TechnicianInfo>();
        try{
            List<EntryInfo> list = repository.findByOrderId(orderid);
            if (list != null){
                for (EntryInfo entry:list){
                    technicianInfo = technicianInfoRepository.findById(entry.getTechnicianId());
                    technicianInfoList.add(technicianInfo);
                }
                technicianInfoList.sort(Comparator.comparing(TechnicianInfo::getScore));
                resultBean.setRows(technicianInfoList);
            }else{
                resultBean.setResultDesc("空");
            }
        }catch(Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    //门店报名关注状态
    public ResultBean storeStatus(String id) {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            //查询出未删除和状态位等于1的该门店的订单信息
            List<OrderList> orderLists=orderListRepository.findByStoreIdAndDelFlagAndFlag(id,0,1);
            EntryAndAttentionNumVo entryAndAttentionNumVo=new EntryAndAttentionNumVo();
            int entrynum=0;
            int attentnum=0;

            for(OrderList ol:orderLists){
                //查询出某个订单下的报名数量
                List<EntryInfo> entryInfos=repository.findByOrderIdAndReadFlagAndDelFlag(ol.getId(),0,0);
                if(entryInfos!=null) {
                    entrynum = entrynum + entryInfos.size();
                }
                //查询出某个订单下的关注数量
                List<AttentionInfo> attentionInfos=attentionInfoRepository.findByOrderIdAndReadFlag(ol.getId(),0);
                if(attentionInfos!=null) {
                    attentnum = attentnum + attentionInfos.size();
                }
            }
            entryAndAttentionNumVo.setAttention(attentnum);
            entryAndAttentionNumVo.setEntry(entrynum);

            resultBean.setObject(entryAndAttentionNumVo);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public ResultBean orderEntry(String id, String orderId) {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            //报名表
            EntryInfo entryInfo = new EntryInfo();
            entryInfo.setOrderId(orderId);
            entryInfo.setTechnicianId(id);
            entryInfo.setRegistrationTime(new Date());
            repository.saveAndFlush(entryInfo);
//            this.add(entryInfo);
            //技师订单信息表
            AcceptInfo acceptInfo = new AcceptInfo();
            //订单id
            acceptInfo.setOrderId(orderId);
            //技师id
            acceptInfo.setTechId(id);
            //已报名状态
            acceptInfo.setFlag(0);
            //创建时间
            acceptInfo.setCreateTime(new Date());
            OrderList orderList = orderListRepository.findById(orderId);
            //订单名称
            acceptInfo.setOrderName(orderList.getSerType());
            //门店id
            acceptInfo.setStoreId(orderList.getStoreId());

        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }

        return resultBean;
    }
}
